<!--添加修改角色信息-->
<template>
    <a-modal v-model="showAddTable" :mask-closable="false" :closable="false" :width="900"
             title="课堂新增"
             :maskClosable="false"
             :destroyOnClose="true">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <a-form-item label="选择课程：" prop="selectScheduleId" v-bind="formStyle">
              <a-select :disabled="!!selectScheduleId"
                  placeholder="请选择课程" v-model="selectScheduleId"
                  v-decorator="['selectScheduleId', { rules: [{ required: true, message: '选择课程' }] }]"
              >
                <a-select-option v-for="item in scheduleSelectList" :value="item.value"
                                 :key="item.value">{{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="实际上课时间：" prop="realStartTime" v-bind="formStyle" required>
              <a-date-picker
                  format="YYYY-MM-DD HH:mm"
                  v-model="realStartTime"
                  showTime
                  placeholder="实际上课时间"
                  style="width: 100%"
                  v-decorator="['realStartTime', { rules: [{ required: true, message: '选择实际上课时间' }] }]"
              />
          </a-form-item>
        </a-form>
        <div slot="footer">
            <a-button type="default" size="default" @click="cancelApply()">取消</a-button>
            <a-button type="primary" size="default" @click="saveApply()">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    import AFormItem from 'ant-design-vue/es/form/FormItem'
    import {validateID} from "@/libs/validate";
    import moment from "moment/moment";
    export default {
        name: 'addEdit',
        components: {
            AFormItem,
        },
        data () {
            return {
                verify,
                form: this.$form.createForm(this),
                showAddTable: false,
                input: false,
              selectScheduleId:"",
              realStartTime:"",
              scheduleSelectList: [],
            }
        },
        computed: {
            formStyle() {
                return this.$store.state.common.formStyle
            }
        },
        props: {

          objDTO:{
            type: Object,
            required: true
          }
        },
        watch: {

        },
        methods: {
          validateID,
            /**
             * Function    : saveApply
             * Description : 打开弹框
             * Input       : none
             * Return      : none
             */
            showModal (opt) {
              this.showAddTable = true
              this.selectScheduleId = opt.scheduleId
              this.id = opt.classTId?opt.classTId:''
              this.realStartTime = opt.realStartTime?moment(opt.realStartTime).format("YYYY-MM-DD HH:mm"):''
              this.$nextTick (() => {
                this.form.setFieldsValue (this.$pick ({selectScheduleId:this.selectScheduleId,realStartTime:this.realStartTime}, [ 'selectScheduleId', 'realStartTime' ]))
              })
            },
            /**
             * Function    : showEdit
             * Description : 编辑时显示
             * Input       : opt单个所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.showAddTable = true
                this.selectScheduleId = opt.scheduleId
                this.id = opt.recordId
                this.realStartTime = moment(opt.realStartTime).format("YYYY-MM-DD HH:mm")
              this.$nextTick (() => {
                this.form.setFieldsValue (this.$pick ({selectScheduleId:this.selectScheduleId,realStartTime:this.realStartTime}, [ 'selectScheduleId', 'realStartTime' ]))
              })
            },

            /**
             * Function    : saveApply
             * Description : 角色信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply ( ) {
                this.form.validateFields((err, value) => {
                    if (!err) {
                        let para = {}
                        para.scheduleId = this.selectScheduleId
                        para.state = '0'
                        para.realStartTime = moment(this.realStartTime).format("YYYY-MM-DD HH:mm")
                      if (!this.title){
                        para.recordId = this.id
                      }
                      this.$store.dispatch ('saveLessonAct', para).then (response => {
                          this.showAddTable = false
                          if (response.code === 0) {
                              this.$emit ('init')
                              this.$message.success (response.message)
                          } else {
                              this.$message.error (response.message)
                          }
                      })
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : val
             * Return      : none
             */
            cancelApply ( ) {
                this.showAddTable = false
                let _this = this
              _this.selectScheduleId = ''
                setTimeout(function() {
                    _this.form.resetFields()
                }, 100)
            },
          getScheduleList(){
            let _this = this;
            _this.$store.dispatch('getScheduleListAct', {courseObject:'1'}).then(response => {
              if (response.code === 0) {
                let data = response.data;
                for (let index in data){
                  let courseTime = moment(data[index].date).format("YYYY-MM-DD")+" "+data[index].attendTime+"-"+data[index].finishTime
                  _this.scheduleSelectList.push({
                    value:data[index].id,
                    label:data[index].teacherName + "  " +data[index].courseName +'【'+courseTime+'】',
                  })
                }
              }else {
                _this.$message.error(response.message)
              }
            })
          },

        },



        created () {
          this.getScheduleList()
        }
    }
</script>
